<?php

namespace App\Listeners;

use App\Events\MTEvent;
use App\Models\UserAssetsModel;
use App\Models\UserMtLogModel;

class MTListener
{
    public function handle(MTEvent $event)
    {

        $info = UserAssetsModel::query()->find($event->user_id);
        if (!$info) {
            $info = new UserAssetsModel();
            $info->user_id = $event->user_id;
        }

        $info->mt += $event->mt;
        $info->saveOrFail();

        $userMtLogModel = new UserMtLogModel();
        $userMtLogModel->user_id = $event->user_id;
        $userMtLogModel->amount = $event->mt;
        $userMtLogModel->remain = $info->mt;
        $userMtLogModel->desc =  $event->desc;
        $userMtLogModel->saveOrFail();

    }
}
